package io.renren.modules.weldingmachineinfo.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mchange.v1.util.StringTokenizerUtils;
import io.renren.modules.welderinfo.service.impl.WelderinfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.weldingmachineinfo.dao.WeldingmachineinfoDao;
import io.renren.modules.weldingmachineinfo.entity.WeldingmachineinfoEntity;
import io.renren.modules.weldingmachineinfo.service.WeldingmachineinfoService;
import io.renren.modules.welderinfo.service.impl.WelderinfoServiceImpl.*;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service("weldingmachineinfoService")
public class WeldingmachineinfoServiceImpl extends ServiceImpl<WeldingmachineinfoDao, WeldingmachineinfoEntity> implements WeldingmachineinfoService {

    @Autowired
    WeldingmachineinfoDao weldingmachineinfoDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WeldingmachineinfoEntity> page = this.page(
                new Query<WeldingmachineinfoEntity>().getPage(params),
                new QueryWrapper<WeldingmachineinfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<WeldingmachineinfoEntity> getwminfo(String wmId, String wmNote) {
        return weldingmachineinfoDao.getwminfo(wmId,wmNote);
    }

    @Override
    public String getGwsn(String wmId) {
        System.out.println(weldingmachineinfoDao.getGwsn(wmId));
        return weldingmachineinfoDao.getGwsn(wmId);
    }

    //1.获取token
    public Object getWmToken() throws JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();

        // 获取token
//        UsersServiceImpl.ReHeader reHeader = new UsersServiceImpl.ReHeader();
        WelderinfoServiceImpl.ReHeader reHeader = new WelderinfoServiceImpl.ReHeader();
        reHeader.setKey("cmhi2023");
        String url = "http://10.142.3.129:8080/getOpenApiToken";
        String result = restTemplate.postForObject(url,reHeader, String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(result);
        String token = String.valueOf(jsonNode.get("data").get("token")).replace("\"","");
        return token.toString();
    }

    //2.获取焊机信息
//获取焊工信息
//    @Async("screenTaskExecutor")
//    @Scheduled(cron = "0/10 * * * * ?")
//    public String getWmInfo() throws InterruptedException, JsonProcessingException, URISyntaxException {
//        String url = "http://10.142.3.129:8080/system/machine/list";
//        String token=getWmToken().toString();
//        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
//        RestTemplate restTemplate = new RestTemplate(requestFactory);
//
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.setBearerAuth(token);
//        RequestEntity<Void> requestEntity = new RequestEntity<>(httpHeaders, HttpMethod.GET,new URI(url));
//        ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
//        String weldmachineinfo = responseEntity.getBody();
////        System.out.println(weldmachineinfo);
//        try {
//            ObjectMapper objectMapper = new ObjectMapper();
//            JsonNode jsonNode = objectMapper.readTree(weldmachineinfo);
//            JsonNode rowsNode = jsonNode.get("rows");
//            if(!jsonNode.isArray()){
//                for (JsonNode rowNode:rowsNode) {
//                    String wmbh = rowNode.get("wmNo").asText();
//                    String wmnote = rowNode.get("wmComment").asText();
//                    String wmbrand = rowNode.get("wmBrand").asText();
//                    String wmmodel = rowNode.get("wmModel").asText();
//                    String wmtype = rowNode.get("wmType").asText();
//                    String colmodel = rowNode.get("collBoxNo").asText();
//                    String wmaddress = rowNode.get("wmMac").asText();
//                    String gwsn = rowNode.get("collModelCode").asText();
////                    String nexttime = rowNode.get("updateTime").asText();
//                    String nexttimeString = rowNode.get("updateTime").asText();
//                    Date nexttime = null;
//                    if (!"null".equals(nexttimeString)) {
//                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                        nexttime = format.parse(nexttimeString);
//                    }
//                    System.out.println("NO:"+wmbh);
//                    System.out.println("描述:"+wmnote);
//                    System.out.println("品牌:"+wmbrand);
//                    System.out.println("型号:"+wmmodel);
//                    System.out.println("类型:"+wmtype);
//                    System.out.println("采集:"+colmodel);
//                    System.out.println("Mac:"+wmaddress);
//                    System.out.println("下次时间:"+nexttime);
////                    saveMysql(we);
//                    System.out.println("------------------");
//                    WeldingmachineinfoEntity weldingmachineinfoEntity = new WeldingmachineinfoEntity();
//                    weldingmachineinfoEntity.setWmbh(wmbh);
//                    weldingmachineinfoEntity.setWmnote(wmnote);
//                    weldingmachineinfoEntity.setWmbrand(wmbrand);
//                    weldingmachineinfoEntity.setWmmodel(wmmodel);
//                    weldingmachineinfoEntity.setWmtype(wmtype);
//                    weldingmachineinfoEntity.setColmodel(colmodel);
//                    weldingmachineinfoEntity.setWmaddress(wmaddress);
//                    weldingmachineinfoEntity.setNexttime(nexttime);
//                    weldingmachineinfoEntity.setGwsn(gwsn);
//                    saveMysql(weldingmachineinfoEntity);
//                }
//            }
//        } catch (Exception e){
//            e.printStackTrace();
//        }
//        return responseEntity.toString();
//    }

    public void saveMysql(WeldingmachineinfoEntity weldingmachineinfoEntity){
        String wmbh = weldingmachineinfoEntity.getWmbh();
        int count = weldingmachineinfoDao.countByBh(wmbh);
        if(count>0){
            System.out.println("已经存在");
        } else {
            weldingmachineinfoDao.addItem(weldingmachineinfoEntity);
        }
        System.out.println("save");
//        log.info("save");
    }
    //3.将焊机信息存入数据库

}